package ywpt.service.config;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.TitleInfo;
import ywpt.web.common.PublicFunction;

public class TitleServiceImpl extends HibernateEntityDao<TitleInfo> implements TitleService{
	private TitleServiceImpl(){
		
	}

	@Override
	public String createTitleCode() {
		DetachedCriteria query = DetachedCriteria.forClass(TitleInfo.class, "title");
		String titleCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<TitleInfo> titleInfos = this.getTitleInfos(query);
		if ( titleInfos.iterator().hasNext() )	
		{
			code = titleInfos.iterator().next().getCode();
			maxId = new Integer(code);
		}
		titleCode=PublicFunction.getNextCode("", maxId, false,3); 
		query.setProjection(null);
		return titleCode;
	}

	@Override
	public TitleInfo getTitleInfoById(Long id) {
		return this.get(id);
	}

	@Override
	public int getTitleInfoCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TitleInfo> getTitleInfos(DetachedCriteria query, int pageSize,
			int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<TitleInfo> getTitleInfos() {
		return this.getAll();
	}

	@Override
	public void removeTitleInfo(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveTitleInfo(TitleInfo titleInfo) {
		this.save(titleInfo);
	}

	@Override
	public void updateTitleInfo(TitleInfo titleInfo) {
		this.update(titleInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TitleInfo> getTitleInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}
} 
